package com.chaodev.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.AttDao;
import com.chaodev.base.dao.CommDao;
import com.chaodev.base.dao.DictDao;
import com.chaodev.base.dto.LunBoDto;
import com.chaodev.base.model.*;
import com.chaodev.base.service.CommService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/18 16:56
 **/
@Service
public class CommServiceImpl implements CommService {

    @Autowired
    CommDao commDao;
    @Autowired
    DictDao dictDao;

    @Override
    public List<Task> getMyWaitTask(Task task) {
        return commDao.getMyWaitTask(task);
//        return null;
    }

    @Override
    public List<Task> getMyDoneTask(Task task) {
        return commDao.getMyDoneTask(task);
//        return null;
    }

    @Override
    public void completeTask(TaskComplete task) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("P_ID", task.getBizId());
        map.put("P_TYPE", task.getBizType());
        map.put("P_OP", task.getOp());
        map.put("p_user", task.getOper());
        map.put("P_cont", task.getDsc());
        commDao.completeTask(map);
        String[] rts = map.get("rt").toString().split(",");
        if ("1".equals(rts[0])) {
            throw new RuntimeException(rts[1]);
        }
    }

    @Override
    public Task getTask(Task task) {
        return commDao.getTask(task);
    }

    @Autowired
    private AttDao attDao;

    @Override
    public Map<String, Object> getData(Task task) {
        Map<String, Object> map = new HashMap<>();
        //表名查询
        String tableName = commDao.getTableNameByBiz(task.getBizType(), task.getBizId());
        //对应的数据查询
        Map<String, Object> os = commDao.selectTableData(tableName, task.getBizId(),task.getBizType());
        //查询所有字段的中文名
        List<Map<String, String>> keys = commDao.selectField(tableName);
        //组新的Map
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> obs = new HashMap<>();
        LocalDateTime localDateTime=null;
        List<TemplateModel> templateModelList = new ArrayList<>();
        Map<String,Object> param=new HashMap<>();
        for (Map<String, String> key : keys) {
            TemplateModel templateModel = new TemplateModel();
            String columnName = key.get("column_name");
            Object value = os.get(columnName);
            templateModel.setName(key.get("lab"));
            templateModel.setColType(key.get("column_type"));

            if(value instanceof LocalDateTime){
                localDateTime=(LocalDateTime)value;
                DateTimeFormatter fm = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                templateModel.setValue(localDateTime.format(fm));
            }else{
                String colType = key.get("column_type");
                if("dict".equals(colType)){
                    String dictId = key.get("dict_id");
                    param=new HashMap<>();
                    param.put("dictId",dictId);
                    param.put("id",value);
                    DictDetail detail = dictDao.getDictDetailInfoById(param);
                    if(detail==null){
                        templateModel.setValue(value);
                    }else{
                        templateModel.setValue(detail.getItemText());
                    }
                }else{
                    templateModel.setValue(value);
                }
            }
            templateModelList.add(templateModel);
        }

        //对应的附件查询
        Att att = new Att();
        att.setBizType(task.getBizType());
        att.setDataNo(task.getBizId());
        att.setState(0);
        List<Att> atts = attDao.queryDataList(att);

        //封装返回
        map.put("att", atts);
        map.put("ob", templateModelList);
        return map;
    }

    @Override
    public Map<String, Object> getVolunteerActivityStatistic(Map<String, Object> map) {
        return commDao.selectVolunteerActivityStatistic(map);
    }

    @Override
    public PageInfo<LinkedHashMap<String, Object>> getLunBoPageList(LunBoDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getRows());
        List<LinkedHashMap<String, Object>> lunBoList = commDao.getLunBoList(dto);
        return new PageInfo<>(lunBoList, dto.getRows());
    }

    @Override
    public void saveLunBo(LunBo lunBo) {
        if (null != lunBo.getId()) {
            commDao.updLunBo(lunBo);
        } else {
            commDao.addLunBo(lunBo);
        }
    }

    @Override
    public void delLunBo(List<Integer> ids) {
        commDao.delLunBo(ids);
    }

    @Override
    public ClockDistance getClockDistance(String departId, String csys) {
        return commDao.getClockDistance(departId, csys);
    }

    @Override
    public void saveClockDistance(ClockDistance distance) {
        if (null != distance.getId()) {
            commDao.updClockDistance(distance);
        } else {
            commDao.addClockDistance(distance);
        }
    }

    @Override
    public void delClockDistance(List<Integer> ids) {
        commDao.delClockDistance(ids);
    }
}
